$(function () {
    var shopId = getQueryString("shopId");
    //说明如果前端传来shopid证明是用来更新内容的 如果不传证明是用来注册店铺信息的
    var isedit = shopId ? true :false;
    // var isedit ;
    // if (shopId == null || shopId == ""){
    //     isedit = false;
    // } else {
    //     isedit = true;
    // }

    var initUrl = '/shopadmin/getshopinitinfo';
    var registerShopUrl = '/shopadmin/registershop';
    var shopInfoUrl = '/shopadmin/getshopbyid?shopId='+shopId;
    var editShopUrl = '/shopadmin/modifyshop';
    // alert(initUrl);
    if(!isedit){
        getShopInitInfo();
    }else {
        getShopInfo(shopId);
    }

    // getShopInitInfo();

    function getShopInfo(shopId) {
        $.getJSON(shopInfoUrl, function(data) {
            // alert(data)
            if (data.success) {
                // alert(data)
                var shop = data.shop;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                var shopCategory = '<option data-id="'
                    + shop.shopCategory.shopCategoryId + '" selected>'
                    + shop.shopCategory.shopCategoryName + '</option>';
                var tempAreaHtml = '';
                data.areaList.map(function(item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                        + item.areaName + '</option>';
                });
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled','disabled');
                $('#shop-area').html(tempAreaHtml);
                $("#shop-area option[data-id='"+shop.area.areaId +"']").attr("selected","selected");
            }
        });
    }

    function getShopInitInfo() {
        $.getJSON(initUrl, function (data) {

            if (data.success) {
                var tempHtml = '';
                var tempAreaHtml = '';
                data.shopCategoryList.map(function (item, index) {
                    tempHtml += '<option data-id= "' + item.shopCategoryId
                        + '">' + item.shopCategoryName + '</option>>';
                });
                data.areaList.map(function (item, index) {
                    tempAreaHtml += '<option data-id="' + item.areaId + '">'
                        + item.areaName + '</option>';
                });
                $('#shop-category').html(tempHtml);
                $('#shop-area').html(tempAreaHtml);
            }
        });
    }

        $('#submit').click(function () {
            var shop = {};
            alert(isedit)
            if (isedit){
                shop.shopId =shopId;
            }

            shop.shopName = $('#shop-name').val();
            shop.shopAddr = $('#shop-addr').val();
            shop.phone = $('#shop-phone').val();
            shop.shopDesc = $('#shop-desc').val();
            shop.shopCategory = {
                shopCategoryId: $('#shop-category').find('option')
                    .not(function () {
                        return !this.selected;
                    }).data('id')
            };


            shop.area = {
                areaId: $('#shop-area').find('option').not(function () {
                    return !this.selected;
                }).data('id')
            };

            var shopImg = $('#shop-img')[0].files[0];
            var formData = new FormData();
            formData.append('shopImg', shopImg);
            formData.append('shopStr', JSON.stringify(shop));
            //验证码部分
            var verifyCodeActual = $("#j_captcha").val();
            if (!verifyCodeActual){
                //如果为空 提示信息
                $.toast("q请输入验证码");
                return;
            }
            formData.append('verifyCodeActual', verifyCodeActual);
            $.ajax({
                url: (isedit ?editShopUrl:registerShopUrl),
                // url: editShopUrl,
                // url: registerShopUrl,
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                cache: false,
                success: function (data) {
                    if (data.success) {
                        $.toast('提交成功 ！');
                    } else {
                        $.toast('提交失败 ！' + data.errMsg);
                    }
                    //无论提交成功还是失败都会刷新验证码
                    $("#captcha_img").click();
                }
            });


        });


})